package org.mindrive.system.util;

import java.util.Random;
import java.util.UUID;

public class UUIDGenerator {
	public UUIDGenerator() {
	}

	/**
	 * 获得一个UUID
	 * 
	 * @return String UUID
	 */
	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
	}

	public static String randomUUID() {
		return UUID.randomUUID().toString();
	}

	/**
	 *
	 * (获取指定长度uuid)
	 * 
	 * @return
	 */
	public static String getUUID(int len) {
		if (0 >= len) {
			return null;
		}
		String uuid = getUUID();
		StringBuffer str = new StringBuffer();

		for (int i = 0; i < len; i++) {
			str.append(uuid.charAt(i));
		}

		return str.toString();
	}

	/**
	 * 获得指定数目的UUID
	 * 
	 * @param number int 需要获得的UUID数量
	 * @return String[] UUID数组
	 */
	public static String[] getUUIDs(int number) {
		if (number < 1) {
			return null;
		}
		String[] ss = new String[number];
		for (int i = 0; i < number; i++) {
			ss[i] = getUUID();
		}
		return ss;
	}

	public static String generateOTP(int length) {
		String numbers = "1234567890";
		Random random = new Random();
		char[] otp = new char[length];

		for (int i = 0; i < length; i++) {
			otp[i] = numbers.charAt(random.nextInt(numbers.length()));
		}
		return String.valueOf(otp);
	}

	private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"
			.split(",");

	private static int getNum(int start, int end) {
		return (int) (Math.random() * (end - start + 1) + start);
	}

	public static String generatePhoneNumber() {
		int index = getNum(0, telFirst.length - 1);
		String first = telFirst[index];
		String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
		String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
		return first + second + third;
	}
}
